﻿using Moven.ViewModels;
using System.Linq;
using static Moven.ViewModels.ArticleWithBookmark;
using System.Collections.Generic;
using System.Threading.Tasks;
using System;

namespace Moven.Views
{
	public partial class SearchPage
	{
		public SearchPage (List<ArticleWithBookmark> allArticles = null)
		{
			InitializeComponent ();
			allArticles = allArticles?? Statics.ArticlesStore.GetArticlesAsync().
				Result.Articles.Select(FromArticle).ToList();
			var vm = new ArticlesViewModel(string.Empty, MenuType.Search, allArticles)
			{
				Navigation = Navigation,
				ArticlesList = listView
			};
            BindingContext = vm;
            searchBar.TextChanged += delegate
            {
                vm.SearchCommand.Execute(null);
            };
        }

		protected async override void OnAppearing()
		{
			base.OnAppearing();
			if (searchBar.Text.IsNullOrEmpty())
			{
				if (IsAndroid && IsTopPage)
				{
					await Task.Delay(TimeSpan.FromMilliseconds(300));
				}
				searchBar.Focus();
			}
		}
    }
}
